package leihua.crm.settings.web.controller;

import leihua.crm.commons.contants.Contants;
import leihua.crm.commons.domain.ReturnObject;
import leihua.crm.commons.utils.DateUtils;
import leihua.crm.settings.domain.User;
import leihua.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author leiHua
 * @Date 2022/10/3 12:18
 */
@Controller
@RequestMapping("/settings/qx/user")
public class UserController {
    @Autowired
    UserService userService;
    /**
     * url 要和controller 方法处理完请求后的 响应返回的页面要和资源目录一致
     * @return
     */
    @RequestMapping("/toLogin.do")  // /settings/qx/user/toLogin.do
    public String login(){
        return "settings/qx/user/login";
    }

    /**
     * 处理登录验证
     * @param loginAct  账号
     * @param loginPwd  密码
     * @param isRemPwd   十天免登录
     * @param request
     * @param session
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/login.do") // /settings/qx/user/login.do
    public Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request,HttpSession session,HttpServletResponse response){
        Map<String,Object> map=new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        ReturnObject returnObject = new ReturnObject();
        User user = userService.selectByActAndPwd(map);
        //code 0 为失败   1为成功
        //登录逻辑判断
        if(user==null){
            //登录失败，用户名或者密码错误
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户名或者密码错误");
        }else { //进一步判断是否合法
            String nowTime = DateUtils.formateDateTime(new Date());
            if(user.getExpireTime().compareTo(nowTime)<0){
                //账号过期
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("登录账号已经过期");
            }else if(!user.getAllowIps().contains(request.getRemoteAddr())){
                // ip地址是否允许
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("ip受限");
            }else if(user.getLockState().equals(Contants.RETURN_OBJECT_CODE_FAIL)){
                //账号被锁定了
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("账号状态被锁定了");
            }else {
                //登录成功
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                session.setAttribute(Contants.SESSION_USER,user);

                //记住密码：
                if("true".equals(isRemPwd)){
                    //添加cookie
                    Cookie c1 = new Cookie("loginAct", user.getLoginAct());
                    c1.setMaxAge(60*60*24*10);
                    response.addCookie(c1);
                    Cookie c2 = new Cookie("loginPwd", user.getLoginPwd());
                    c2.setMaxAge(60*60*24*10);
                    response.addCookie(c2);
                }else{
                    //销毁cookie
                    Cookie c1 = new Cookie("loginAct", "");
                    c1.setMaxAge(0);
                    response.addCookie(c1);
                    Cookie c2 = new Cookie("loginPwd", "");
                    c2.setMaxAge(0);
                    response.addCookie(c2);
                }

            }
        }
        return returnObject;
    }

    @RequestMapping("logout.do")  //  /settings/qx/user/logout.do
    public String logout(HttpSession session,HttpServletResponse response){
        //销毁cookie
        Cookie c1 = new Cookie("loginAct", "");
        c1.setMaxAge(0);
        response.addCookie(c1);
        Cookie c2 = new Cookie("loginPwd", "");
        c2.setMaxAge(0);
        response.addCookie(c2);
        session.invalidate();
        return "redirect:/";
    }
}
